<?php 
namespace Wdl\validate;

class Validate{

    protected $rule = [];
    protected $message =[] ;
    protected $filter = [];
    protected $scene = null;
    protected $sence_name = '';

   
    public static function make( $rule = null, $message = null){
        $self =  new static();
        $rule && $self->set_rules($rule);
        $message && $self->set_message($message);
        return $self;
    }

    public function get_scene()
    {
        return $this->scene;
    }

    public function get_rule()
    {
        return $this->rule;
    }
    public function get_message(){
        return $this->message;
    }
    public function set_message($message){
        $this->message = $message ?? [];
    }
    public function set_rules($rule )  {
        $this->rule = $rule ?? [];
    }

    public function  scene($name ){
        $this->sence_name = $name;
        return $this;
    }

    public function check($data)
    {
        if(empty($this->get_rule())){
            return  $data;
        }
        $fields = [];
        $fields = array_keys($this->get_rule());
        $sences = $this->get_scene();
        $rulesArr = $this->get_rule();
        
        if($this->sence_name && !empty($sences ) && isset($sences[$this->sence_name])){
            $fields = $sences[$this->sence_name];
        }
        $return_data = [];
        foreach($fields as $field ){
            $item =  $rulesArr[ $field ];
            $rules = explode("|", $item);
            foreach($rules as $rule_name){
                $message =   $this->get_check_message($field, $rule_name) ;
                $value =  $data[$field] ?? '';
                $return_data[$field] = $value;
                switch($rule_name){
                    case 'required':
                        if( empty($value) )
                            throw new ValidateException( $message);
                        break;
                    case 'mobile':
                        if(!$this->checkIsMobile( $value)){
                            throw new ValidateException( $message);
                        }
                        break;
                    case 'min':                        
                        break;
                    default :
                    break;
                }            
            }
        }
        return $return_data;
    }

    static public function checkIsMobile($mobile) {
        if (!is_numeric($mobile)) {
          return false;
        }
        return preg_match('/^1[2345789]\d{9}$/', $mobile) ? true : false;
    }

    protected function get_check_message($field, $rule_name)
    {
        $message = $this->get_message();
        if($message && isset($message[ $field .".". $rule_name ])){
            return $message[ $field .".". $rule_name ] ;
        }
        
        switch($rule_name){
            case 'required':
                $msg = $field . '必须填写';
                break;
            default:
                $msg = $field . '验证失败';
            break;
        }
        
        return  $msg;
    }
}